package wu.example.orderserver.controller;

import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.rpc.RpcContext;
import org.apache.dubbo.rpc.cluster.loadbalance.ConsistentHashLoadBalance;
import org.apache.dubbo.rpc.cluster.loadbalance.RandomLoadBalance;
import org.apache.dubbo.rpc.cluster.loadbalance.RoundRobinLoadBalance;
import org.apache.dubbo.rpc.cluster.support.*;
import org.apache.dubbo.rpc.cluster.support.FailoverCluster;
import org.apache.dubbo.rpc.cluster.support.MergeableCluster;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import wu.example.orderserver.invoker.MyDubboCluster;
import wu.example.orderserver.lb.WuLoadBalance;
import wu.example.orderserver.service.UserServiceMock;
import wu.example.serviceapi.service.UserService;

/**
 * @author benjamin_5
 * @Description
 * @date 2022/11/19
 */
@RestController
public class OrderController {

    @DubboReference(filter = "auth,log")
    private UserService userService;

    @Value("${server.port}")
    private Integer port;

    @GetMapping("getUser")
    public String getUser(){
        String userName = userService.getUserById(port);
        return userName + " getUser success";
    }

    @GetMapping("createOrder")
    public String createOrder(Integer id, String token){
        try{
            RpcContext.getClientAttachment().setAttachment("auth", token);
            String userName = userService.getUserById(id);
            String res = RpcContext.getServerContext().getAttachment("res");
            return userName + " createOrder success, "+res;
        }catch (Exception e){
            e.printStackTrace();
            return "createOrder fail: " + e.getMessage();
        }
    }

    @GetMapping("getInfo")
    public String getInfo(){
        return "调用"+userService.getInfo();
    }
}
